<template>
  <div>
    <el-card style="width: 100%">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <div class="app-space-between">
          <div class="app-space-between-left">
            <!-- <el-form-item label="用户:" prop="thirdOrderId">
              <el-input v-model="queryParams.thirdOrderId" placeholder="请输入用户" />
            </el-form-item> -->
          </div>
          <div class="app-space-between-right">
            <el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
            <el-button size="mini" @click="resetQuery">重置</el-button>
          </div>
        </div>
      </el-form>

      <el-row class="mb8">
        <el-col :span="1.5" style="margin-right: 10px">
          <el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <div style="height: 16px"></div>

      <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
        <el-table-column show-overflow-tooltip label="序号" type="index" width="50" align="center" />
        <el-table-column show-overflow-tooltip label="用户" min-width="140" align="center" prop="username" />
        <el-table-column show-overflow-tooltip label="角色" min-width="140" align="center" prop="roleName" />
        <el-table-column show-overflow-tooltip label="邮箱" min-width="150" align="center" prop="mail" />
        <el-table-column show-overflow-tooltip label="操作" fixed="right" width="140" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
            <el-button type="text" v-if="currentUser.username != scope.row.username" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" /> -->
      <user-add ref="user-add" @success="getList"></user-add>
    </el-card>
  </div>
</template>

<script>
import UserAdd from "./components/user-add.vue";
import { listUser, delUser } from "@/api/setting/user";
export default {
  components: {
    UserAdd,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        thirdOrderId: "",
        phone: "",
        couponTypeEnum: "",
        mealCardNumber: "",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    async getList() {
      try {
        this.loading = true;
        let res1 = await listUser(this.queryParams);
        // console.log(res1.resultValue);
        this.postList = res1.resultValue.users;
        this.currentUser = res1.resultValue.currentUser
        // this.total = res1.total;
        this.loading = false;
      } catch (e) {
        this.loading = false;
      }
    },
    /** 新增 */
    handleAdd() {
      this.$refs["user-add"].init();
    },
    /** 编辑 */
    handleEdit(row) {
      if (this.ids.length != 1 && !row.username)
        return this.$message.error("请选择一个数据进行编辑");
      let postIds = row.username || this.ids[0];
      this.$refs["user-add"].init(postIds);
    },
    /** 删除 */
    handleDel(row) {
      let postIds = row.username || this.ids;
      if (this.ids.length != 1 && !row.username)
        return this.$message.error("请选择要删除的数据");
      if (Array.isArray(postIds)) postIds = postIds.toString();
      this.$confirm("确认删除用户吗？删除后，数据不能恢复。", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return delUser({}, {
            username: postIds
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.username);
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["queryForm"].resetFields();
      this.handleQuery();
    },
    /** 导出按钮操作 */
    async handleExport() {
      this.download(
        "/lgb/purchaseHistory/export",
        {
          ...this.queryParams,
        },
        `老干部消费_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
.brother+.brother {
  margin-left: 6px;
}

.table-price {
  text-align: left;
  padding-left: 40px;
}
</style>
